package org.zjvis.graph.analysis.service.dto;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.zjvis.datascience.common.graph.vo.CategoryVO;
import org.zjvis.datascience.common.graph.vo.EdgeVO;
import org.zjvis.datascience.common.graph.vo.LinkVO;
import org.zjvis.datascience.common.graph.vo.NodeVO;
import org.zjvis.graph.analysis.service.util.DozerUtil;
import org.zjvis.graph.analysis.service.vo.*;

import java.util.ArrayList;

@Data
public class GraphDTO extends BaseDTO {
    private Long id;

    private String name;

    private Long projectId;

    private Long userId;

    private String dataJson;

    public GraphVO view() {
        GraphVO vo = DozerUtil.mapper(this, GraphVO.class);
        JSONObject jsonObject = JSONObject.parseObject(dataJson);
        vo.setCategories(jsonObject.getJSONArray("categories").toJavaList(CategoryVO.class));
        vo.setEdges(jsonObject.getJSONArray("edges").toJavaList(EdgeVO.class));
        vo.setNodes(jsonObject.getJSONArray("nodes").toJavaList(NodeVO.class));
        vo.setLinks(jsonObject.getJSONArray("links").toJavaList(LinkVO.class));
        vo.setGraphInfo(jsonObject.getJSONObject("graphInfo"));
        vo.setActionContext(jsonObject.getJSONObject("actionContext"));
        return vo;
    }

    public GraphVO initActionView() {
        GraphVO vo = DozerUtil.mapper(this, GraphVO.class);
        vo.setCategories(new ArrayList<>());
        vo.setEdges(new ArrayList<>());
        return vo;
    }

    public void initActionView(GraphVO vo) {
        vo.setId(this.getId());
        vo.setName(this.getName());
        vo.setProjectId(this.getProjectId());
        vo.setUserId(this.getUserId());
        vo.setCategories(new ArrayList<>());
        vo.setEdges(new ArrayList<>());
        vo.setNodes(new ArrayList<>());
        vo.setLinks(new ArrayList<>());
    }

    //仅返回左边元素
    public GraphVO leftView() {
        GraphVO vo = DozerUtil.mapper(this, GraphVO.class);
        JSONObject jsonObject = JSONObject.parseObject(dataJson);
        vo.setCategories(jsonObject.getJSONArray("categories").toJavaList(CategoryVO.class));
        vo.setEdges(jsonObject.getJSONArray("edges").toJavaList(EdgeVO.class));
        return vo;
    }

    public GraphVO tab() {
        GraphVO tab = new GraphVO();
        tab.setId(id);
        tab.setName(name);
        return tab;
    }

}
